home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / HTTP / Status.pm < prev   
Encoding:
Perl POD Document  |  1999-12-28  |  4.9 KB  |  196 lines

  1.  
  2. package HTTP::Status;
  3.  
  4. require 5.002;   # becase we use prototypes
  5.  
  6. =head1 NAME
  7.  
  8. HTTP::Status - HTTP Status code processing
  9.  
  10. =head1 SYNOPSIS
  11.  
  12.  use HTTP::Status;
  13.  
  14.  if ($rc != RC_OK) {
  15.      print status_message($rc), "\n";
  16.  }
  17.  
  18.  if (is_success($rc)) { ... }
  19.  if (is_error($rc)) { ... }
  20.  if (is_redirect($rc)) { ... }
  21.  
  22. =head1 DESCRIPTION
  23.  
  24. I<HTTP::Status> is a library of routines for defining and
  25. classification of HTTP status codes for libwww-perl.  Status codes are
  26. used to encode the overall outcome of a HTTP response message.  Codes
  27. correspond to those defined in RFC 2068.
  28.  
  29. =head1 CONSTANTS
  30.  
  31. The following constant functions can be used as mnemonic status code
  32. names:
  33.  
  34.    RC_CONTINUE                (100)
  35.    RC_SWITCHING_PROTOCOLS        (101)
  36.  
  37.    RC_OK                (200)
  38.    RC_CREATED                (201)
  39.    RC_ACCEPTED                (202)
  40.    RC_NON_AUTHORITATIVE_INFORMATION    (203)
  41.    RC_NO_CONTENT            (204)
  42.    RC_RESET_CONTENT            (205)
  43.    RC_PARTIAL_CONTENT            (206)
  44.  
  45.    RC_MULTIPLE_CHOICES            (300)
  46.    RC_MOVED_PERMANENTLY            (301)
  47.    RC_MOVED_TEMPORARILY            (302)
  48.    RC_SEE_OTHER                (303)
  49.    RC_NOT_MODIFIED            (304)
  50.    RC_USE_PROXY                (305)
  51.  
  52.    RC_BAD_REQUEST            (400)
  53.    RC_UNAUTHORIZED            (401)
  54.    RC_PAYMENT_REQUIRED            (402)
  55.    RC_FORBIDDEN                (403)
  56.    RC_NOT_FOUND                (404)
  57.    RC_METHOD_NOT_ALLOWED        (405)
  58.    RC_NOT_ACCEPTABLE            (406)
  59.    RC_PROXY_AUTHENTICATION_REQUIRED    (407)
  60.    RC_REQUEST_TIMEOUT            (408)
  61.    RC_CONFLICT                (409)
  62.    RC_GONE                (410)
  63.    RC_LENGTH_REQUIRED            (411)
  64.    RC_PRECONDITION_FAILED        (412)
  65.    RC_REQUEST_ENTITY_TOO_LARGE        (413)
  66.    RC_REQUEST_URI_TOO_LARGE        (414)
  67.    RC_UNSUPPORTED_MEDIA_TYPE        (415)
  68.  
  69.    RC_INTERNAL_SERVER_ERROR        (500)
  70.    RC_NOT_IMPLEMENTED            (501)
  71.    RC_BAD_GATEWAY            (502)
  72.    RC_SERVICE_UNAVAILABLE        (503)
  73.    RC_GATEWAY_TIMEOUT            (504)
  74.    RC_HTTP_VERSION_NOT_SUPPORTED    (505)
  75.  
  76. =cut
  77.  
  78.  
  79.  
  80. require Exporter;
  81. @ISA = qw(Exporter);
  82. @EXPORT = qw(is_info is_success is_redirect is_error status_message);
  83. @EXPORT_OK = qw(is_client_error is_server_error);
  84.  
  85.  
  86. my %StatusCode = (
  87.     100 => 'Continue',
  88.     101 => 'Switching Protocols',
  89.     200 => 'OK',
  90.     201 => 'Created',
  91.     202 => 'Accepted',
  92.     203 => 'Non-Authoritative Information',
  93.     204 => 'No Content',
  94.     205 => 'Reset Content',
  95.     206 => 'Partial Content',
  96.     300 => 'Multiple Choices',
  97.     301 => 'Moved Permanently',
  98.     302 => 'Moved Temporarily',
  99.     303 => 'See Other',
  100.     304 => 'Not Modified',
  101.     305 => 'Use Proxy',
  102.     400 => 'Bad Request',
  103.     401 => 'Unauthorized',
  104.     402 => 'Payment Required',
  105.     403 => 'Forbidden',
  106.     404 => 'Not Found',
  107.     405 => 'Method Not Allowed',
  108.     406 => 'Not Acceptable',
  109.     407 => 'Proxy Authentication Required',
  110.     408 => 'Request Timeout',
  111.     409 => 'Conflict',
  112.     410 => 'Gone',
  113.     411 => 'Length Required',
  114.     412 => 'Precondition Failed',
  115.     413 => 'Request Entity Too Large',
  116.     414 => 'Request-URI Too Large',
  117.     415 => 'Unsupported Media Type',
  118.     500 => 'Internal Server Error',
  119.     501 => 'Not Implemented',
  120.     502 => 'Bad Gateway',
  121.     503 => 'Service Unavailable',
  122.     504 => 'Gateway Timeout',
  123.     505 => 'HTTP Version Not Supported',
  124. );
  125.  
  126. my $mnemonicCode = '';
  127. my ($code, $message);
  128. while (($code, $message) = each %StatusCode) {
  129.     $message =~ tr/a-z \-/A-Z__/;
  130.     $mnemonicCode .= "sub RC_$message () { $code }\t";
  131.     $mnemonicCode .= "push(\@EXPORT, 'RC_$message');\n";
  132. }
  133. eval $mnemonicCode; # only one eval for speed
  134. die if $@;
  135.  
  136. =head1 FUNCTIONS
  137.  
  138. The following additional functions are provided.  Most of them are
  139. exported by default.
  140.  
  141. =over 4
  142.  
  143. =item status_message($code)
  144.  
  145. The status_message() function will translate status codes to human
  146. readable strings. The string is the same as found in the constant
  147. names above.
  148.  
  149. =cut
  150.  
  151. sub status_message ($)
  152. {
  153.     return undef unless exists $StatusCode{$_[0]};
  154.     $StatusCode{$_[0]};
  155. }
  156.  
  157. =item is_info($code)
  158.  
  159. Return TRUE if C<$code> is an I<Informational> status code.
  160.  
  161. =item is_success($code)
  162.  
  163. Return TRUE if C<$code> is a I<Successful> status code.
  164.  
  165. =item is_redirect($code)
  166.  
  167. Return TRUE if C<$code> is a I<Redirection> status code.
  168.  
  169. =item is_error($code)
  170.  
  171. Return TRUE if C<$code> is an I<Error> status code.  The function
  172. return TRUE for both client error or a server error status codes.
  173.  
  174. =item is_client_error($code)
  175.  
  176. Return TRUE if C<$code> is an I<Client Error> status code.  This
  177. function is B<not> exported by default.
  178.  
  179. =item is_server_error($code)
  180.  
  181. Return TRUE if C<$code> is an I<Server Error> status code.   This
  182. function is B<not> exported by default.
  183.  
  184. =back
  185.  
  186. =cut
  187.  
  188. sub is_info         ($) { $_[0] >= 100 && $_[0] < 200; }
  189. sub is_success      ($) { $_[0] >= 200 && $_[0] < 300; }
  190. sub is_redirect     ($) { $_[0] >= 300 && $_[0] < 400; }
  191. sub is_error        ($) { $_[0] >= 400 && $_[0] < 600; }
  192. sub is_client_error ($) { $_[0] >= 400 && $_[0] < 500; }
  193. sub is_server_error ($) { $_[0] >= 500 && $_[0] < 600; }
  194.  
  195. 1;
  196.